import type { RouteRecordRaw } from 'vue-router'

//  动态路由

export function mapMenusToRoutes(userMenus: any[]){
  // 动态添加路由====================================================================
  const localRouter: RouteRecordRaw[] = []
    //读取router目录下home的所有ts文件
  const  file: Record<string, any> =  import.meta.glob('@/router/home/**/*.ts', {eager: true})
    // 循环将对象加到路由列表里面
  for (const key in file) {
    const module = file[key]
    localRouter.push(module.default)
  }
  // 根据菜单去匹配路由
  const routes: RouteRecordRaw[] = []
  for (const menu of userMenus) {
    for (const sub_menu in menu.sub_menus) {
      const route = localRouter.find(item => item.path === menu.sub_menus[sub_menu].path)
      if (route) routes.push(route)
    }
  }
  return routes
}
